package com.lanou;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

/**
 * Created by admin on 22/1/18.
 */
@Controller
@RequestMapping("/ajax")
public class AjaxController {
    @Resource
    private DataSource dataSource;

    @Resource
    private JdbcTemplate jdbcTemplate;


    //@RequestMapping("hello")// htttp://localhost:8888/hello
    @RequestMapping("hello")// htttp://localhost:8888/hello
    @ResponseBody //响应ajax请求，需要添加的注解
    public boolean logindo(HttpServletRequest request, String uname, String upwd) throws Exception {
        boolean islogin = false;
        // List<User> users = new ArrayList<>();
        Connection connection = dataSource.getConnection();
        PreparedStatement ps = connection.prepareStatement("select * from user_info where uname=? and upwd = ?");
        ps.setString(1, uname);
        ps.setString(2, upwd);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
//            User user = new User();
//            user.setUsername(rs.getString("user_name"));
//            user.setPassword(rs.getString("pass_word"));
//            users.add(user);
            String name = rs.getString("uname");
            String pwd = rs.getString("upwd");
            String uintroduce = rs.getString("uintroduce");
            if (name.equals(uname) && pwd.equals(upwd)) {
                islogin = true;
                HttpSession session = request.getSession();
                session.setAttribute("uname", uname);
                session.setAttribute("upwd", upwd);
                session.setAttribute("uintroduce", uintroduce);
                break;
            }
        }
        return islogin;
    }
//    public Users login() {
//String sql = "";
//
//        return null;
//    }

    @RequestMapping("zhuce")// htttp://localhost:8888/hello
    //@RequestMapping("/dataSource")
    @ResponseBody //响应ajax请求，需要添加的注解

    public ModelAndView zhuce(String zhanghao, String mima, String jieshao, ModelAndView modelAndView) {
//        try {
//            Connection connection = dataSource.getConnection();
//            PreparedStatement ps = connection.prepareStatement("insert into user_info (uname ,upwd,uintroduce)  values (?,?,?);");
//            ps.setString(1, zhanghao);
//            ps.setString(2, mima);
//            ps.setString(3, jieshao);
//            ps.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "";
        jdbcTemplate.update("insert into user_info (uname ,upwd,uintroduce)  values (?,?,?);", zhanghao, mima, jieshao);
        return modelAndView;

    }

    @RequestMapping("quit")// htttp://localhost:8888/hello
    //@RequestMapping("/dataSource")
    @ResponseBody //响应ajax请求，需要添加的注解

    public String quit(HttpServletRequest request) {

        HttpSession session = request.getSession();


        session.invalidate();
        return "";
    }


    @RequestMapping("lol")// htttp://localhost:8888/hello
    //@RequestMapping("/dataSource")
    //@ResponseBody //响应ajax请求，需要添加的注解

    public ModelAndView lol(String name, ModelAndView modelAndView, String username, HttpServletRequest request) {

        String sql = "select d1,d2,d3,img1,img2,img3 from ugame where name = ? ";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        User u = jdbcTemplate.queryForObject(sql, rowMapper, name);
        modelAndView.addObject("d1", u.getD1());
        modelAndView.addObject("d2", u.getD2());
        modelAndView.addObject("d3", u.getD3());
        modelAndView.addObject("img1", u.getImg1());
        modelAndView.addObject("img2", u.getImg2());
        modelAndView.addObject("img3", u.getImg3());

        HttpSession session = request.getSession();
        username = (String) session.getAttribute("uname");

        String sql1 = "select uid from user_info where uname = ?";
        RowMapper<Users> rowMapper1 = new BeanPropertyRowMapper<>(Users.class);
        Users us = jdbcTemplate.queryForObject(sql1, rowMapper1, username);

        int uid = us.getUid();
        String sql2 = "select money from usergame where uid = ? and gid = 1";
        RowMapper<UserGame> rowMapper2 = new BeanPropertyRowMapper<>(UserGame.class);
        List<UserGame> uu = jdbcTemplate.query(sql2, rowMapper2, uid);
        int money = 0;
        if (uu.size() > 0) {
            money = uu.get(0).getMoney();
            modelAndView.addObject("money", money);
        } else {
            jdbcTemplate.update("insert into usergame (uid ,gid,money)  values (?,?,0);", uid, 1);
            modelAndView.addObject("money", money);

        }

        session.setAttribute("gid", 1);
        session.setAttribute("uid", us.getUid());
        session.setAttribute("money", money);
        modelAndView.setViewName("lol");
        return modelAndView;
    }

    @RequestMapping("dandan")// htttp://localhost:8888/hello
    //@RequestMapping("/dataSource")
    //@ResponseBody //响应ajax请求，需要添加的注解

    public ModelAndView dandna(ModelAndView modelAndView, String name, String username, HttpServletRequest request) {
        String sql = "select d1,d2,d3,img1,img2,img3 from ugame where name = ?";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        User u = jdbcTemplate.queryForObject(sql, rowMapper, name);
        modelAndView.addObject("d1", u.getD1());
        modelAndView.addObject("d2", u.getD2());
        modelAndView.addObject("d3", u.getD3());
        modelAndView.addObject("img1", u.getImg1());
        modelAndView.addObject("img2", u.getImg2());
        modelAndView.addObject("img3", u.getImg3());

        HttpSession session = request.getSession();
        username = (String) session.getAttribute("uname");

        String sql1 = "select uid from user_info where uname = ?";
        RowMapper<Users> rowMapper2 = new BeanPropertyRowMapper<>(Users.class);
        Users us1 = jdbcTemplate.queryForObject(sql1, rowMapper2, username);

        int uid = us1.getUid();

        String sql2 = "select money from usergame where uid = ? and gid = 3";
        RowMapper<UserGame> rowMapper3 = new BeanPropertyRowMapper<>(UserGame.class);
        List<UserGame> ug = jdbcTemplate.query(sql2, rowMapper3, uid);
        int money = 0;

        if (ug.size() > 0) {
            money = ug.get(0).getMoney();
            modelAndView.addObject("money", money);
        } else {
            jdbcTemplate.update("insert into usergame (uid ,gid,money)  values (?,?,0);", uid, 3);
            modelAndView.addObject("money", money);
        }


//        String sq1 = "select udandanm from user_info where uname = ?";
//        RowMapper<Users> rowMapper1 = new BeanPropertyRowMapper<>(Users.class);
//        Users us = jdbcTemplate.queryForObject(sq1, rowMapper1, username);
//
//        modelAndView.addObject("money", us.getUdandanm());
//
        session.setAttribute("gid", 3);
        session.setAttribute("uid", us1.getUid());
        session.setAttribute("money", money);
        modelAndView.setViewName("lol");
        return modelAndView;
    }

    @RequestMapping("chi")// htttp://localhost:8888/hello
    //@RequestMapping("/dataSource")
    // @ResponseBody //响应ajax请求，需要添加的注解
//
    public ModelAndView chi(ModelAndView modelAndView, String name, String username, HttpServletRequest request) {
        String sql = "select d1,d2,d3,img1,img2,img3 from ugame where name = ?";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        User u = jdbcTemplate.queryForObject(sql, rowMapper, name);
        modelAndView.addObject("d1", u.getD1());
        modelAndView.addObject("d2", u.getD2());
        modelAndView.addObject("d3", u.getD3());
        modelAndView.addObject("img1", u.getImg1());
        modelAndView.addObject("img2", u.getImg2());
        modelAndView.addObject("img3", u.getImg3());


        HttpSession session = request.getSession();
        username = (String) session.getAttribute("uname");

        String sql1 = "select uid from user_info where uname = ?";

        RowMapper<Users> rowMapper1 = new BeanPropertyRowMapper<>(Users.class);
        Users us = jdbcTemplate.queryForObject(sql1, rowMapper1, username);
        int uid = us.getUid();

        String sql2 = "select money from usergame where uid = ? and gid =2";
        RowMapper<UserGame> rowMapper2 = new BeanPropertyRowMapper<>(UserGame.class);
        List<UserGame> uu = jdbcTemplate.query(sql2, rowMapper2, uid);
        int money = 0;
        if (uu.size() > 0) {
            money = uu.get(0).getMoney();
            modelAndView.addObject("money", money);

        } else {
            jdbcTemplate.update("insert into usergame (uid ,gid,money)  values (?,?,0);", uid, 2);
            modelAndView.addObject("money", money);
        }
        session.setAttribute("gid", 2);
        session.setAttribute("uid", us.getUid());
        session.setAttribute("money", money);
        modelAndView.setViewName("lol");

        return modelAndView;
    }


    @RequestMapping("recharge")// htttp://localhost:8888/hello
    @ResponseBody
    public String recharge(String jine, HttpServletRequest request) {
        HttpSession session = request.getSession();
        int money1 = (int) session.getAttribute("money");
        int uid = (int) session.getAttribute("uid");
        int gid = (int) session.getAttribute("gid");
        int jine1 = Integer.parseInt(jine);
        int money = jine1 + money1;
        String sql = "  update usergame set money = ? where uid = ? and gid = ?   ";


        jdbcTemplate.update(sql,money,uid,gid);




        session.setAttribute("money", money);

        return "lol";


    }







}
